<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Galery extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(2);
		$this->load->model('Company_galery_model', 'galery');
		$this->lang->load('company/galery', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$images_list = $this->galery->get(array('idmember' => $this->session->userdata['idmember']));
		
		$grid_images = array();
        
		foreach ($images_list->result() as $row)
		{
			$grid_images[] = '<div class="row"><span><span class="img-galery"><span class="img-border fleft vmiddle">
			<img class="pointer" onclick="CompanyGalery.viewPhoto(\''.$row->image.'\');" src="'.base_url() . "f/" . $this->session->userdata['idmember'] . '/img/' . $row->thumbnail.'" /></span></span>
			<span class="fleft vmiddle box-description"><h3>'.$row->title.'</h3><p>'.nl2br($row->description).'</p></span>
			</span>
			<div class="gtool">
			<a onclick="CompanyGalery.viewPhoto(\''.$row->image.'\');">'.$this->lang->line('galery_viewphoto').'</a>
			 &middot; <a onclick="CompanyGalery.editPhoto('.$row->id.');">'.$this->lang->line('galery_updatetext').'</a>
			 &middot; <a onclick="CompanyGalery.deletePhoto('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
		}
		
		if (!$grid_images) {
			 $data['grid_images'] = '<center>'.$this->lang->line('galery_noresults').'</center>';
		} else {
			 $data['grid_images'] = implode('<hr/>', $grid_images);
		}
		
		exitHTML($data['grid_images']);
	}
	
	public function insert()
	{
		if (is_numeric($_POST['idphoto']))
		{
			return $this->update();
		}
		
		if ($_FILES['image']['error'] == 4)
		{
			formError($this->lang->line('galery_missingfield'), true, true);
		}
		
		if (!in_array($_FILES['image']['type'], array('image/gif', 'image/jpeg', 'image/png', 'image/wbmp')))
		{
			formError($this->lang->line('galery_invalidimageformat'), true, true);
		}
		
		if ($_FILES['image']['size'] > (1024 * 1024 * 1)) // 1Mb
		{
			formError(sprintf($this->lang->line('galery_invalidimagesize'), '1 Mb'), true, true);
		}
		
		$data['image'] = $_FILES['image']['tmp_name'];
		$data['image_name'] = $_FILES['image']['name'];
		$data['idmember'] = $this->session->userdata['idmember'];
		$data['key'] = $this->session->userdata['key'];
		$data['title'] = $_POST['title'];
		$data['description'] = $_POST['description'];
		
		if ($this->galery->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true, true);
		}
		else
		{
			exit('<script type="text/javascript">parent.CompanyGalery.getGaleryGrid();parent.closeBox();</script>');
		}
	}
	
	public function update()
	{
		$data = $_POST;
		
		$data['idmember'] = $this->session->userdata['idmember'];
		
		if ($this->galery->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exit('<script type="text/javascript">parent.CompanyGalery.getGaleryGrid();parent.closeBox();</script>');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('images_missingfield'));
		}
		
		if ($this->galery->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CompanyGalery.getGaleryGrid();');
		}
	}
	
	public function edit($id_photo = null)
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		$data['id_photo'] = $id_photo;
		
		$data['title'] = '';
		$data['description'] = '';
				
		if (is_numeric($id_photo))
		{
			$photo_data = $this->galery->get(array('idphoto' => $id_photo))->row();
			$data['title'] = $photo_data->title;
			$data['description'] = $photo_data->description;
			$data['edition'] = true;
		}
		
		$data['userdata'] = $this->session->userdata;
        $this->load->view('company/galery', $data);
	}
	
	public function view($photo_name = '')
	{
		header("Content-type: text/html; charset=utf-8");
		
		$html = '<img src="'.base_url().'f/'.$this->session->userdata['idmember'].'/img/'.$photo_name.'" />';
		
		exitHTML($html);
	}	
	
	public function add()
	{
		$this->edit();
	}
	
	public function capture()
	{
		header("Content-type: text/html; charset=utf-8");
		$data['userdata'] = $this->session->userdata;
        $this->load->view('company/galery_capture', $data);
	}
	
	public function upload()
	{
		header("Content-type: text/html; charset=utf-8");
		if ($_POST['type'] == "pixel") {
	        $im = imagecreatetruecolor(600, 400);
	
	        foreach (explode("|", $_POST['image']) as $y => $csv) {
                foreach (explode(";", $csv) as $x => $color) {
                	imagesetpixel($im, $x, $y, $color);
                }
	        }
		} else {
	        $im = imagecreatefrompng($_POST['image']);
		}
		
		$tmp_path = FCPATH . 'tmp/' . $this->session->userdata['key'] . '_capture';
		
		if (!is_dir($tmp_path))
		{
			mkdir($tmp_path);
		}
		
		$tmp_file = $tmp_path . '/image.png';
		
		imagepng($im, $tmp_file, 9);
		
		$data['tmp_path'] = $tmp_path;
		$data['tmp_file'] = $tmp_file;
		$data['image_name'] = 'image.png';
		$data['idmember'] = $this->session->userdata['idmember'];
		$data['key'] = $this->session->userdata['key'];
		
		if ($this->galery->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true, true);
		}
		else
		{
			exitJs('CompanyGalery.getGaleryGrid();closeBox();');
		}
	}
}

?>